﻿using System;
using System.Collections.Generic;
using System.Text;
using Daco.Web.Store;

namespace Daco.Web.Task
{
    public class CUploadPhotoTask : Daco.Tasks.CLongBackgroundTask
    {
        private string _storePath;
        private int _selectedIndex;
        private EPhotoType _photoType;
        private List<Daco.Web.UI.WebControls.FlashUploadItem> _items;
        private string _photoTypeID;

        public CUploadPhotoTask(string photoTypeID, EPhotoType photoType, IEnumerable<Daco.Web.UI.WebControls.FlashUploadItem> items, int selectedIndex, string storePath)
            : base()
        {
            _photoTypeID = photoTypeID;
            _photoType = photoType;
            _items = new List<Daco.Web.UI.WebControls.FlashUploadItem>(items);
            _selectedIndex = selectedIndex;

            _storePath = storePath;
            //if (!_storePath.EndsWith("/")) _storePath = _storePath + "/";
        }

        public override void RunOnce()
        {
            try
            {
                int itemIndex = 0;
                foreach (Daco.Web.UI.WebControls.FlashUploadItem item in _items)
                {
                    if (!item.IsStored)
                    {
                        string filePath = _storePath + item.FileName;
                        if (System.IO.File.Exists(filePath))
                        {

                            // Open image file
                            System.IO.FileStream imageStream = System.IO.File.Open(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

                            byte[] imageContent = new byte[imageStream.Length];
                            imageStream.Read(imageContent, 0, imageContent.Length);

                            // Release resources
                            imageStream.Close();
                            imageStream.Dispose();

                            byte[] thumbImage = null;
                            byte[] largeImage = null;

                            // calculate image size
                            using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageContent)))
                            {
                                if (oldImage.Size.Width > Config.CSiteConfig.Current.ImageSection.ThumbImageSize || oldImage.Size.Height > Config.CSiteConfig.Current.ImageSection.ThumbImageSize)
                                {
                                    thumbImage = Daco.Utils.CHelper.ResizeImageFile(imageContent, Config.CSiteConfig.Current.ImageSection.ThumbImageSize);
                                }
                                else
                                {
                                    thumbImage = imageContent;
                                }

                                if (oldImage.Size.Width > Config.CSiteConfig.Current.ImageSection.LargeImageSize || oldImage.Size.Height > Config.CSiteConfig.Current.ImageSection.LargeImageSize)
                                {
                                    largeImage = Daco.Utils.CHelper.ResizeImageFile(imageContent, Config.CSiteConfig.Current.ImageSection.LargeImageSize);
                                }
                                else
                                {
                                    largeImage = imageContent;
                                }

                                oldImage.Dispose();
                            }

                            bool primary = false;
                            if (itemIndex == _selectedIndex) primary = true;
                            else
                                primary = false;

                            string trueFileName = item.FileName.Substring(item.FileName.IndexOf('_') + 1);

                            if (Daco.Web.Config.CSiteConfig.Current.ImageSection.StoreImageInDB)
                            {
                                Daco.Web.Store.CPhoto.AddPhoto(
                                    _photoTypeID,
                                    string.Empty,
                                    Daco.Web.Config.CSiteConfig.Current.ImageSection.PrefixStringForThumbImage + trueFileName,
                                    thumbImage,
                                    trueFileName,
                                    largeImage,
                                    primary);
                            }

                            itemIndex++;
                        }

                    }
                }
            }
#if DEBUG
            catch(Exception ex)
            {
                string msg = ex.Message;
            }
#else
            catch
            {
            }
#endif
        }
    }
}
